package com.lightsaber.trade.as.web.archive;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.entity.archive.ArchivedImage;
import com.lightsaber.trade.as.entity.planning.Planning;
import com.lightsaber.trade.as.service.archive.ArchiveImageService;
import com.lightsaber.trade.as.service.archive.DuplicateNameException;
import com.lightsaber.trade.as.service.category.CategoryService;
import com.lightsaber.trade.as.service.planning.PlanningService;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/archive")
public class ArchiveAction extends BaseAction {
    private static final long serialVersionUID = -7076736990225879052L;

    @Autowired
    private PlanningService planningService;

    @Autowired
    private ArchiveImageService archiveImageService;

    @Autowired
    private CategoryService categoryService;

    private Planning planning;

    private ArchivedImage archivedImage;

    // ****************** Page parameters *****************START
    private boolean search;

    @Value(value = "#{appProperties['archive.page.size']}")
    private String pageSize;

    private Page<Planning> page;

    // ****************** Page parameters *****************END

    @Override
    public String execute() throws Exception {
        if (search) {
            if (page == null) {
                page = new Page<Planning>(Integer.valueOf(pageSize));
            } else {
                page.setPageSize(Integer.valueOf(pageSize));
            }

            if (!page.isOrderBySetted()) {
                page.setOrderBy(BaseEntity.ID);
                page.setOrder(Page.ASC);
            }
            archiveImageService.findPage(planning, page);

        } else {
            planning = null;
        }

        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        planning = planningService.get(planning.getId());
        return INPUT;
    }

    public String archiveImage() throws Exception {
        try {
            planning = planningService.get(planning.getId());
            archiveImageService.archiveImage(planning, archivedImage);
            addActionMessage(getText(PROCESSED_SUCCESS));
        } catch (DuplicateNameException ex) {
            this.addActionError("Duplicate Name: " + archivedImage.getName());
        }

        return INPUT;
    }

    public Planning getPlanning() {
        return planning;
    }

    public void setPlanning(final Planning planning) {
        this.planning = planning;
    }

    public boolean isSearch() {
        return search;
    }

    public void setSearch(final boolean search) {
        this.search = search;
    }

    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(final String pageSize) {
        this.pageSize = pageSize;
    }

    public Page<Planning> getPage() {
        return page;
    }

    public void setPage(final Page<Planning> page) {
        this.page = page;
    }

    public ArchivedImage getArchivedImage() {
        return archivedImage;
    }

    public void setArchivedImage(final ArchivedImage archivedImage) {
        this.archivedImage = archivedImage;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(final CategoryService categoryService) {
        this.categoryService = categoryService;
    }

}
